package com.mst.controller;

import com.mst.domain.User;
import com.mst.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("")
    @ResponseBody
    public String index(String name, Integer age) {
        System.out.println("姓名" + name);
        System.out.println("年龄" + age);
        userService.print();
        return "success";
    }

    @RequestMapping("/params")
    @ResponseBody
    public String params(User user, @RequestParam(value = "username", required = true, defaultValue = "默认") String name) {
        System.out.println(name);
        System.out.println("姓名：" + user.getName());
        System.out.println("年龄：" + user.getAge());
        userService.print();
        return "success";
    }

    @RequestMapping("/arr")
    @ResponseBody
    public String arrParams(String[] arrs) {
        for (int i = 0; i < arrs.length; i++) {
            System.out.println(arrs[i]);
        }
        return "success";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String arrList(@RequestParam List<String> ids) {
        ids.forEach(id -> System.out.println(id));
        return "success";
    }

    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name, Integer age) {
        System.out.println("name=" + name + ",age=" + age);
        return "success";
    }

    @RequestMapping("/objParam")
    @ResponseBody
    public String objParam(User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/objForJson")
    @ResponseBody
    public String objForJson(@RequestBody User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/lisss")
    @ResponseBody
    public String lisss(@RequestBody List<User> users) {
        users.forEach(user -> System.out.println(user));
        return "success";
    }
}
